<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2020/1/9
 * Time: 17:46
 */

namespace app\user\home;

use app\common\controller\ApiBase;
use app\user\model\UserFans;
use app\user\validate\FansValidate;

/**
 * 粉丝模块
 * Class Fans
 * @package app\user\home
 */
class Fans extends ApiBase
{
    // 全部接口必须先授权
    protected $beforeActionList = ['checkIsLogin'];

    /**
     * 添加关注
     * @return \think\response\Json
     * @throws \app\common\exception\ApiError
     */
    public function focus()
    {
        $input = $this->getInput("obj_uid", 1);
        (new FansValidate)->goCheck($input);

        $fans = new UserFans;
        $fans->user_id = $input["user_id"];
        $fans->obj_uid = $input["obj_uid"];
        $fans->save();

        return $this->apiSuccess("关注成功！");
    }

    /**
     * 取消关注
     * @return \think\response\Json
     * @throws \app\common\exception\ApiError
     */
    public function unfocus()
    {
        $input = $this->getInput("obj_uid", 1);

        $fans = UserFans::get([
            "user_id" => $input["user_id"],
            "obj_uid" => $input["obj_uid"]
        ]);

        if (!$fans)
            return $this->apiError("没有内容被取消..");

        $fans->delete();
        return $this->apiSuccess("取消成功！");
    }
}